package com.bwie.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdAttaMapper;
import com.bwie.mapper.TbProdInfoMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdAtta;
import com.bwie.pojo.TbProdInfo;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author Lenovo
 * @description 针对表【tb_prod(商品表)】的数据库操作Service实现
 * @createDate 2025-07-21 19:43:21
 */
@Service
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
        implements TbProdService {

    @Autowired
    private TbProdMapper tbProdMapper;

    @Autowired
    private TbProdAttaMapper tbProdAttaMapper;

    @Autowired
    private TbProdInfoMapper tbProdInfoMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public R getProdList(PageInfoVo pageInfoVo) {
        Page<TbProd> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbProd> tbProdPage = tbProdMapper.getProdList(page, pageInfoVo);
        return R.OK(tbProdPage);
    }

    @Override
    public R addProd(TbProd tbProd) {
        TbProd tbProd1 = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName()));
        if (tbProd1 != null) {
            return R.ERROR(500, "商品已存在");
        }
        tbProdMapper.insert(tbProd);
        return R.OK();
    }

    @Override
    public R addProdAtta(TbProdAtta tbProdAtta) {
        TbProd tbProd = tbProdMapper.selectById(tbProdAtta.getProdId());
        if (tbProd == null) {
            return R.ERROR(500, "商品不存在");
        }

        tbProdAttaMapper.delete(new QueryWrapper<TbProdAtta>().lambda().eq(TbProdAtta::getProdId, tbProdAtta.getProdId()));

        tbProdAttaMapper.insert(tbProdAtta);
        return R.OK();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatchProd(List<Integer> ids) {
        tbProdMapper.deleteBatchIds(ids);

        tbProdAttaMapper.delete(new QueryWrapper<TbProdAtta>().lambda().in(TbProdAtta::getProdId, ids));

        return R.OK();
    }

    @Override
    public R deleteByProdId(Integer prodId) {
        TbProd tbProd = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdId, prodId));
        //tbProd包含了商品里的所有数据
        if (tbProd == null) {
            return R.ERROR(500, "商品不存在");
        }
        tbProdMapper.deleteById(prodId);

        return R.OK();
    }

    @Override
    public R getProdByProdId(Integer prodId) {
        TbProd tbProd = tbProdMapper.selectById(prodId);
        //tbProd包含了商品里的所有数据
        if (tbProd == null) {
            return R.ERROR(500, "商品不存在");
        }
        return R.OK(tbProd);
    }

    @Override
    public R getProdInfoList(PageInfoVo pageInfoVo) {
        Page<TbProdInfo> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbProdInfo> wrapper = new QueryWrapper<>();
        if (pageInfoVo.getProdId() != null) {
            wrapper.lambda().eq(TbProdInfo::getProdId, pageInfoVo.getProdId());
        }
        wrapper.lambda().orderByAsc(TbProdInfo::getProdId);
        Page<TbProdInfo> tbProdInfoPage = tbProdInfoMapper.selectPage(page, wrapper);

        return R.OK(tbProdInfoPage);
    }

    @Override
    public R addProdInfo(TbProdInfo tbProdInfo) {
        TbProdInfo tbProdInfo1 = tbProdInfoMapper.selectOne(new QueryWrapper<TbProdInfo>().lambda().eq(TbProdInfo::getProdName, tbProdInfo.getProdName()));
        if (tbProdInfo1 != null) {
            return R.ERROR(500, "商品名称已存在");
        }

        TbProdInfo prodInfo = tbProdInfoMapper.selectOne(new QueryWrapper<TbProdInfo>().lambda().eq(TbProdInfo::getProdSku, tbProdInfo.getProdSku()));
        if (prodInfo != null) {
            return R.ERROR(500, "商品SKU已存在");
        }
        String token = request.getHeader("token");
        tbProdInfo.setCreateBy(TokenUtil.getUserName(token));
        tbProdInfo.setCreateTime(new Date());
        tbProdInfoMapper.insert(tbProdInfo);

        return R.OK();
    }
}




